import 'dart:math';

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:largefriends/pages/lf_home/lf_home_logic.dart';
import 'package:largefriends/res.dart';
import 'package:largefriends/res/lf_color.dart';
import 'package:largefriends/res/strings/lf_strings.dart';
import 'package:largefriends/widgets/bottom_navigation_bar/DotNavigationBarItem.dart';
import 'package:largefriends/widgets/bottom_navigation_bar/NavBars.dart';
import 'package:largefriends/widgets/dialog_action_tip.dart';
import 'package:largefriends/widgets/preload_page_view.dart';

class LfHomePage extends StatefulWidget {
  const LfHomePage({super.key});

  @override
  State<LfHomePage> createState() => _LfHomePageState();
}

class _LfHomePageState extends State<LfHomePage> {
  final logic = Get.put(LfHomeLogic());
  final List<_SelectableNavItem> _items = List.empty(growable: true);

  @override
  void initState() {
    _items
      ..add(_SelectableNavItem(
        selectedIcon: Res.tab_flame,
        unselectedIcon: Res.tab_flame,
      ))
      ..add(_SelectableNavItem(
        selectedIcon: Res.tab_search,
        unselectedIcon: Res.tab_search,
      ))
      ..add(_SelectableNavItem(
        selectedIcon: Res.tab_message,
        unselectedIcon: Res.tab_message,
      ))
      ..add(_SelectableNavItem(
        selectedIcon: Res.tab_contact,
        unselectedIcon: Res.tab_contact,
      ))
      ..add(_SelectableNavItem(
        selectedIcon: Res.tab_me,
        unselectedIcon: Res.tab_me,
      ));
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return WillPopScope(
      child: Scaffold(
        key: logic.scaffoldKey,
        appBar: null,
        resizeToAvoidBottomInset: true,
        extendBody: true,
        bottomNavigationBar: Obx(
          () {
            return DotNavigationBar(
              onTap: (index) {
                logic.pageController.jumpToPage(index);
                logic.selectedIndex.value = index;
              },
              currentIndex: logic.selectedIndex.value,
              enablePaddingAnimation: false,
              dotIndicatorColor: Colors.transparent,
              items: List.generate(_items.length, (index) {
                var item = _items[index];
                var num = 0;
                if (index == 2) {
                  num = max(0, logic.badgeEntity.value.newMessage);
                } else if (index == 3) {
                  num = max(
                      0,
                      logic.badgeEntity.value.newLikedMe +
                          logic.badgeEntity.value.newVisitedMe);
                }
                return DotNavigationBarItem(
                    icon: _icon(
                        icon: logic.selectedIndex.value == index
                            ? item.selectedIcon
                            : item.unselectedIcon,
                        num: num,
                        color: logic.selectedIndex.value == index
                            ? colorFF007F
                            : color514F62),
                    selectedColor: colorFF007F);
              }),
            );
          },
        ),
        body: PreloadPageView(
          controller: logic.pageController,
          preloadPagesCount: logic.pages.length,
          physics: const NeverScrollableScrollPhysics(),
          children: logic.pages,
          onPageChanged: (index) {},
        ),
      ),
      onWillPop: () async {
        showActionTips(
            actionButtonTip: strExit.tr,
            tips: strWantToExitThisApp.tr,
            action: () async {
              await SystemChannels.platform.invokeMethod<void>(
                'SystemNavigator.pop',
              );
            });
        return false;
      },
    );
  }
}

Widget _icon({required String icon, required int num, required color}) {
  return Badge.count(
    count: num,
    isLabelVisible: num > 0,
    textStyle: TextStyle(fontSize: 12.sp, color: Colors.white),
    textColor: colorFEFEFE,
    backgroundColor: colorFF007F,
    child: Image.asset(
      icon,
      width: 28.r,
      height: 28.r,
      fit: BoxFit.cover,
      color: color,
    ),
  );
}

class _SelectableNavItem {
  final String unselectedIcon;
  final String selectedIcon;
  _SelectableNavItem({
    required this.selectedIcon,
    required this.unselectedIcon,
  });
}
